﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace CreateRepoDB
{
    class Program
    {
        static string CreateUser()
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLServer"].ConnectionString);
            SqlCommand comm;
            string commandText = @" IF NOT EXISTS (SELECT loginname FROM master.dbo.syslogins WHERE name='WMIUser')
                                    BEGIN
                                        USE [master]
                                        CREATE LOGIN [WMIUser] WITH PASSWORD=N'WMIUser', DEFAULT_DATABASE=[Mynte], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
                                        USE [Mynte]
                                        CREATE USER [WMIUser] FOR LOGIN [WMIUser]
                                        use [master]
                                        GRANT ALTER ANY DATABASE TO [WMIUser]
                                        GRANT AUTHENTICATE SERVER TO [WMIUser]
                                        GRANT CONNECT SQL TO [WMIUser]
                                        GRANT CREATE ANY DATABASE TO [WMIUser]
                                        GRANT EXTERNAL ACCESS ASSEMBLY TO [WMIUser]
                                        GRANT VIEW ANY DATABASE TO [WMIUser]
                                        GRANT VIEW ANY DEFINITION TO [WMIUser]
                                        PRINT 'DONE: USER WMIUser WAS CREATED'
                                    END
                                    ELSE
                                        PRINT 'USER WMIUser ALREADY EXISTS'
                                    ";
            string status = string.Empty;
            try
            {
                using (conn)
                {
                    conn.Open();
                    comm = new SqlCommand(commandText, conn);
                    using (comm)
                    {
                        comm.ExecuteNonQuery();
                    }
                }
                status = "DONE";
            }
            catch (Exception ex)
            {
                status = ex.Message;
            }
            return status;
        }

        static string CreateDataBase()
        {
            string commandText = @"IF NOT EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name='Mynte')
                                   BEGIN
	                                   CREATE DATABASE Mynte
                                       PRINT 'DONE: DATABASE Mynte WAS CREATED'
                                   END
                                   ELSE
	                                   PRINT 'DATABASE ALDREADY EXISTS'";
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLServer"].ConnectionString);
            SqlCommand comm;
            string status = string.Empty;

            try
            {
                using (conn)
                {
                    conn.Open();
                    comm = new SqlCommand(commandText, conn);
                    using (comm)
                    {
                        comm.ExecuteScalar();
                    }
                }
                status = "DONE";
            }
            catch (Exception ex)
            {
                status = ex.Message;
            }
            return status;
        }

        static void Main(string[] args)
        {
            Console.WriteLine(CreateDataBase());
            Console.WriteLine(CreateUser());
            Console.ReadKey();
        }
    }
}
